<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-29
 * Time: 13:48
 */

namespace backend\controllers;


use app\models\CrmMallOrder;
use app\models\CrmMallProduct;
use app\models\CrmMallProductClassify;
use app\models\CrmMallProductSpecify;
use app\models\search\CrmMallProductSearch;
use yii\helpers\ArrayHelper;

class CrmProductController extends CommonController
{
    //查询 商品列表
    public function actionProductList(){//查询
        $res = $this->getList(new CrmMallProductSearch(),$_POST['page'],$_POST['limit'],['like','dlxl','"'.is_set($_POST,'id').'"']);
        return $res;
    }
    //条件查询 商品列表
    public function actionProductSearchList(){//查询
        $model = new CrmMallProductSearch();
        $lists = $model->search(\Yii::$app->request->post());
        $limit = is_set($_POST,"limit");
        $offset = is_set($_POST,"limit")*(is_set($_POST,"page")-1);
        $lists->query->limit($limit)->offset($offset);
        return $this->actionLayData($lists->count,ArrayHelper::toArray($lists->query->all()),0,'',is_set($_POST,"page"));
    }
    //查询 产品分类父id为0的 大类
    public function actionDlList(){//查询
        $classify = new CrmMallProductClassify();
        $res = $classify->find()->select("flmc as label,id")->andWhere(['parent_id'=>0])->asArray()->all();
        return $this->dealFD($this->actionElementData($res));
    }
    //查询 产品分类父id父亲id的 小类
    public function actionXlList(){//查询
        $classify = new CrmMallProductClassify();
        $res = $classify->find()->select("flmc as label,id")->andWhere(['parent_id'=>$_POST['parent_id']])->asArray()->all();
        return $this->dealFD($res);
    }
    //保存商品
    public function actionSave(){
        try{
            $product = new CrmMallProduct();
            $post = \Yii::$app->request->post();
            $p = $post['CrmMallProduct'];
            $dlxl = is_set($p,'dlxl');
            $post['CrmMallProduct']['dlxl'] = json_encode($dlxl);
            if (is_set($p,'id')!=""){//修改
                $product = CrmMallProduct::findOne($p['id']);
                if($product->updateAttributes($post['CrmMallProduct'])){
                    return $this->dealFD("修改成功!");
                }else{
                    return $this->dealFD($product->getErrors());
                }
            }else{//新增
                if ($product->load($post) && $product->save()){
                    return $this->dealFD("新增成功!");
                }else{
                    return $this->dealFD($product->getErrors());
                }
            }
        }catch (\Exception $e){
            return $this->dealFD($e);
        }
    }
    //删除商品
    public function actionDelete(){
        $product = CrmMallProductSearch::findOne($_POST['id']);
        if ($product->delete()){
            return $this->dealFD("删除成功!");
        }else{
            return $this->dealFD($product->getErrors());
        }
    }
    //查询所有商品分类
    public function actionAllClassify(){
        $classify = new CrmMallProductClassify();
        $list = $classify->find()->select("flmc as label,id,parent_id")->asArray()->all();
        return $this->dealFD($this->getElementTree($list,0));
    }
    //新增商品分类
    public function actionAddClassify(){
        $classify = new CrmMallProductClassify();
        if ($classify->load($_POST) && $classify->save()){
            return $this->dealFD($classify->attributes);
        }else{
            return $this->dealFD($classify->getErrors());
        }
    }
    //删除商品分类
    public function actionDeleteClassify(){
        $classify = CrmMallProductClassify::findOne($_POST['id']);
        if ($classify->delete()){
            $this->deleteAllChildren($_POST['id']);
            return $this->dealFD("删除商品分类成功!");
        }else{
            return $this->dealFD($classify->getErrors());
        }
    }
    public function deleteAllChildren($id){
        $s = CrmMallProductClassify::findAll(['parent_id'=>$id]);
        foreach ($s as $k=>$v){
            $v->delete();
            $this->deleteAllChildren($v->id);
        }
        return true;
    }

    /**
     * 查询商品规格
     * @return bool
     */
    public function actionSpggList(){
        $s = CrmMallProductSpecify::findAll(['product_id'=>$_POST['product_id']]);
        return $this->actionLayData(count($s),ArrayHelper::toArray($s));
    }
    /**
     * 商品规格删除
     * @return string
     */
    public function actionSpggDelete(){
        $specify = CrmMallProductSpecify::findOne($_POST['id']);
        if ($specify->delete()){
            $this->deleteAllChildren($_POST['id']);
            return $this->dealFD("删除商品规格成功!");
        }else{
            return $this->dealFD($specify->getErrors());
        }
    }
    //保存商品
    public function actionSpggSave(){
        $specify = new CrmMallProductSpecify();
        $post = \Yii::$app->request->post();
        $p = $post['CrmMallProductSpecify'];
        if (is_set($p,'id')!=""){//修改
            $specify = CrmMallProductSpecify::findOne($p['id']);
            if($specify->updateAttributes($post['CrmMallProductSpecify'])){
                return $this->dealFD("修改成功!");
            }else{
                return $this->dealFD($specify->getErrors());
            }
        }else{//新增
            if ($specify->load($post) && $specify->save()){
                return $this->dealFD("新增成功!");
            }else{
                return $this->dealFD($specify->getErrors());
            }
        }
    }


}